﻿using System;
using System.Web.Mvc;
using System.Linq;
using Web.Infrastructure.Compression;

namespace Web.Controllers
{
    public partial class SharedController : Controller
    {
        public virtual ActionResult SiteMap()
        {
            return PartialView("SiteMap.generated");
        }

        public virtual ActionResult UploadImage()
        {
            var name = string.Empty;
            if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
            {
                //IF IE
                if (Request.Files[0].FileName.Contains("\\"))
                {
                    var count = Request.Files[0].FileName.Split('\\').Count();
                    name = Guid.NewGuid() + Request.Files[0].FileName.Split('\\')[count - 1];
                }
                else
                {
                    name = Guid.NewGuid() + Request.Files[0].FileName;
                }
                Request.Files[0].SaveAs(Server.MapPath("~/Uploads/") + name);
            }
            Response.ContentType = "text/plain";
            Response.Write(name);
            //Do not use JSON Faking IE open it like file
            return new EmptyResult();
        }

        public virtual ActionResult FileUpload()
        {
            var name = string.Empty;
            if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
            {
                //IF IE
                if (Request.Files[0].FileName.Contains("\\"))
                {
                    var count = Request.Files[0].FileName.Split('\\').Count();
                    name = Guid.NewGuid() + Request.Files[0].FileName.Split('\\')[count - 1];
                }
                else
                {
                    name = Guid.NewGuid() + Request.Files[0].FileName;
                }
                Request.Files[0].SaveAs(Server.MapPath("~/Uploads/") + name);
            }
            Response.ContentType = "text/plain";
            Response.Write(name);
            //Do not use JSON Faking IE open it like file
            
            return new EmptyResult();
        }

        public virtual ActionResult Compress()
        {
            var compressor = new Compressor(Url);
            compressor.StartCompress();

            return new EmptyResult();
        }
    }
}